package com.gopay.bis.dao.payconfig.impl;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import org.springframework.stereotype.Repository;

import com.gopay.bis.dao.payconfig.BisExpaySmsSendLogDAO;
import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.domain.log.BisExpaySmsSendLog;
import com.gopay.common.util.DateUtils;

/** 
 * 快捷支付发送校验短信日志DAO
 * @ClassName: BisExpaySmsSendLogDAOImpl 
 * @author w_jiqing 
 * @date 2016年12月1日 上午11:02:03  
 */ 
@Repository("bisExpaySmsSendLogDAO")
public class BisExpaySmsSendLogDAOImpl extends BaseDAOImpl<BisExpaySmsSendLog, Integer> implements BisExpaySmsSendLogDAO {

    @Override
    public int findToDayCountByMobileNum(String mobNum, String moduleID) {
        /**
         * modified by zyt,对加密字段查询使用in查询
         */
        //start++++++++++
        //String hql = "SELECT COUNT(*) FROM BisExpaySmsSendLog S WHERE S.mobileNum=:mobileNum AND S.moduleId=:moduleId AND S.createdTime >= to_timestamp(:createdTime,'yyyy-MM-dd')";
        String hql = "SELECT COUNT(*) FROM BisExpaySmsSendLog S WHERE " +
                "S.mobileNum in (:hisMobileNumList) AND S.moduleId=:moduleId AND S.createdTime >= to_timestamp(:createdTime,'yyyy-MM-dd')";
        Map<String, Object> param = new HashMap<String,Object>();
        //param.put("mobileNum", mobNum);
        param.put("hisMobileNumList", SensitiveInfoUtils.getAllHisCipherText(mobNum).toArray());
        //end++++++++++++
        param.put("moduleId", moduleID);
        param.put("createdTime", DateUtils.format(new Date(),"yyyy-MM-dd"));
        return this.findCountByHql(hql, param);
    }

    @Override
    public int findToDayCountByOrderId(String orderId, String moduleID) {
        String hql = "SELECT COUNT(*) FROM BisExpaySmsSendLog S WHERE S.orderId=:orderId AND S.moduleId=:moduleId AND S.createdTime >= to_timestamp(:createdTime,'yyyy-MM-dd')";
        Map<String, Object> param = new HashMap<String,Object>();
        param.put("orderId", orderId);
        param.put("moduleId", moduleID);
        param.put("createdTime", DateUtils.format(new Date(),"yyyy-MM-dd"));
        return this.findCountByHql(hql, param);
    }

    @Override
    public BisExpaySmsSendLog getBisExpaySmsSendLog(String orderId, String moduleID) {
        String hql = "FROM BisExpaySmsSendLog S WHERE S.orderId = :orderId AND S.moduleId=:moduleId AND S.createdTime >= to_timestamp(:createdTime,'yyyy-MM-dd') order by S.createdTime desc ";
        Map<String, Object> param = new HashMap<String,Object>();
        param.put("orderId", orderId);
        param.put("moduleId", moduleID);
        param.put("createdTime", DateUtils.format(new Date(),"yyyy-MM-dd"));
        List<BisExpaySmsSendLog> list = this.findByHql(hql, param);
        if(list != null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public int findToDayCountByOrderIdCardNO(String orderId, String cardNo, String moduleID) {
        /**
         * modified by zyt,对加密字段查询使用in查询
         */
        //start++++++++++
        //String hql = "SELECT COUNT(*) FROM BisExpaySmsSendLog S WHERE S.orderId=:orderId AND S.cardNo=:cardNo AND S.moduleId=:moduleId AND S.createdTime >= to_timestamp(:createdTime,'yyyy-MM-dd')";
        String hql = "SELECT COUNT(*) FROM BisExpaySmsSendLog S WHERE S.orderId=:orderId AND " +
                "S.cardNo in (:hisCardNoList) AND S.moduleId=:moduleId AND S.createdTime >= to_timestamp(:createdTime,'yyyy-MM-dd')";
        Map<String, Object> param = new HashMap<String,Object>();
        param.put("orderId", orderId);
        //param.put("cardNo", cardNo);
        param.put("hisCardNoList", SensitiveInfoUtils.getAllHisCipherText(cardNo).toArray());
        //end++++++++++++
        param.put("moduleId", moduleID);
        param.put("createdTime", DateUtils.format(new Date(),"yyyy-MM-dd"));
        return this.findCountByHql(hql, param);
    }

    @Override
    public BisExpaySmsSendLog getRemarkIsNotNull(String orderId, String moduleID) {
        String hql = "FROM BisExpaySmsSendLog S WHERE S.orderId = :orderId AND S.moduleId=:moduleId AND S.createdTime >= to_timestamp(:createdTime,'yyyy-MM-dd') AND S.remark is not null order by S.createdTime desc ";
        Map<String, Object> param = new HashMap<String,Object>();
        param.put("orderId", orderId);
        param.put("moduleId", moduleID);
        param.put("createdTime", DateUtils.format(new Date(),"yyyy-MM-dd"));
        List<BisExpaySmsSendLog> list = this.findByHql(hql, param);
        if(list != null && list.size()>0){
            return list.get(0);
        }
        return null;
    }



}
